/**
 * 
 */
package com.monkeyboy.security.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;

/**
 * @Description
 *
 * @author Gavin<br>
 *         2019年8月23日
 */
@RestController
public class UserController {

	@GetMapping("/user/getMe")
	public Object getMeInfo(Authentication user, HttpServletRequest request) {
		// 1.参数中通过注解@AuthenticationPrincipal UserDetails user
		// SecurityContextHolder.getContext().getAuthentication();获取springsecurity中的用户信息
		// 2. 通过参数 Authentication authentication获得信息
		// 3. 通过UserDetails
		String header = request.getHeader("Authorization");
		String token = StringUtils.substringAfter(header, "bearer");
		try {
			Claims claims = Jwts.parser().setSigningKey("springsecurity".getBytes("utf-8"))// 要设置签名密钥才能解析
					.parseClaimsJws(token).getBody();
			String company = (String) claims.get("company");
			System.err.println("----:" + company);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;
	}
}
